/**
 * @description 国家编辑弹窗
 * @author 李艳
 */

import React, {Component} from 'react'
import {Form, Input, InputNumber, Checkbox, Modal} from 'antd';
import {checkInputCode} from "../../../commons/utils/CommonUtils";

const FormItem = Form.Item;

class EditCountryModal extends Component {
  constructor(props) {
    super(props);
    this.state = {
      modalVisible: false,
      confirmLoading: false,
    }
  }

  handleClose = () => {
    this.props.form.resetFields();
  };

  componentDidMount() {
    this.props.onRef(this);
  }

  render() {
    const formItemLayout = {
      labelCol: {
        span: 8
      },
      wrapperCol: {
        span: 15
      },
    };

    const {confirmLoading, modalVisible, handleOk, handleCancel, defaultValue, isAdd} = this.props;
    const {getFieldDecorator} = this.props.form;
    let title = "编辑";
    let FormValue = defaultValue;
    if (isAdd) {
      title = "新增";
      FormValue = {}
    }
    return (
      <div>
        <Modal title={title}
               visible={modalVisible}
               onOk={handleOk}
               onCancel={handleCancel}
               width={600}
               afterClose={this.handleClose}
               confirmLoading={confirmLoading}
               maskClosable={false}
        >
          <FormItem
            style={{display: "none"}}
            label="id">
            {getFieldDecorator('id', {
              initialValue: FormValue.id ? FormValue.id : "",
            })(
              <Input/>
            )}
          </FormItem>
          <FormItem
            {...formItemLayout}
            label="代码">
            {getFieldDecorator('code', {
              initialValue: FormValue.code ? FormValue.code : "",
              rules: [{required: true, message: '请填写代码!'},checkInputCode()]
            })(
              <Input maxLength={4}/>
            )}
          </FormItem>

          <FormItem
            {...formItemLayout}
            label="名称">
            {getFieldDecorator('name', {
              initialValue: FormValue.name ? FormValue.name : "",
              rules: [{required: true, message: '请填写名称!'}]
            })(
              <Input/>
            )}
          </FormItem>
          <FormItem
            {...formItemLayout}
            label="国家货币代码">
            {getFieldDecorator('currencyCode', {
              initialValue: FormValue.currencyCode ? FormValue.currencyCode : "",
              rules: [{required: true,
                max:5,
                message: '请填写国家货币代码(最多5位）!'}]
            })(
              <Input/>
            )}
          </FormItem>
          <FormItem
            {...formItemLayout}
            label="国家货币名称">
            {getFieldDecorator('currencyName', {
              initialValue: FormValue.currencyName ? FormValue.currencyName : "",
              rules: [{required: true, message: '请填写国家货币名称!'}]
            })(
              <Input/>
            )}
          </FormItem>
          <FormItem
            {...formItemLayout}
            label="排序">
            {getFieldDecorator('rank', {
              initialValue: FormValue.rank >= 0 ? FormValue.rank : "",
            })(
              <InputNumber precision={0} min={0} max={999999999} style={{width: "100%"}}/>
            )}
          </FormItem>
          <FormItem
            {...formItemLayout}
            label="是否国外">
            {getFieldDecorator('toForeign', {
              valuePropName: 'checked',
              initialValue: FormValue.toForeign,
            })(
              <Checkbox/>
            )}
          </FormItem>
        </Modal>
      </div>
    );
  }
}

EditCountryModal = Form.create()(EditCountryModal);
export default EditCountryModal;
